using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbLoaiMHDAL
    {
       private SqlConnection cn = null;
        public tbLoaiMHDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbLoaiMHDAL(tbLoaiMHDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbLoaiMH loaimh)
       {
           SqlCommand cm = new SqlCommand("tbLoaiMH_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Ten", loaimh .Ten);
           cm.ExecuteNonQuery();
       }
       public void Update(tbLoaiMH loaimh)
       {
           SqlCommand cm = new SqlCommand("tbLoaiMH_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaLoaiMH", loaimh.MaLoaiMH);
           cm.Parameters.AddWithValue("@Ten", loaimh.Ten);
           cm.ExecuteNonQuery();
       }
       public void Delete(int ID)
       {
           SqlCommand cm = new SqlCommand("tbLoaiMH_Detele", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaLoaiMH", ID);
           cm.ExecuteNonQuery();

       }
       public tbLoaiMHCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbLoaiMH_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbLoaiMHCollection loaiMHCollection = new tbLoaiMHCollection();
           while (dr.Read())
           {
               tbLoaiMH loaiMH = new tbLoaiMH();
               loaiMH .MaLoaiMH  = int.Parse(dr.GetValue(0).ToString());
               loaiMH .Ten  = dr.GetValue(1).ToString();
               loaiMHCollection.Add(loaiMH);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return loaiMHCollection;


       }
    }
}
